<?php 
namespace app\admin\controller;
use think\Controller;
use catetree\Catetree;

class Article extends Controller{

	public function index(){
		$dataRes = db('article')->alias('a')->field('a.*,c.cate_name')->join('cate c','a.cate_id=c.id')->order("a.id desc")->paginate(15);
		$this->assign([
			'dataRes'	=>$dataRes,
		]);
		return view();
	}

	public function add(){
		$cate = new Catetree();
		if(request()->isPost()){
			$data = input('post.');
			$data['addtime'] = time();
			if($_FILES['thumb']['tmp_name']){
				$data['thumb'] = $this->upload();
			}
			$dataRes = db('article')->insert($data);
			if($dataRes){
				$this->success('添加文章成功！',url('admin/Article/index'));
			}else{
				$this->error('添加文章失败！');
			}
		}
		$cateRes = db('cate')->order("sort desc")->select();
		$cateRes = $cate->catetree($cateRes);
		$this->assign([
			'cateRes'	=>$cateRes,
		]);
		return view();
	}

	public function edit($id){
		$cate = new Catetree();
		$dataRes = db('article')->find($id);
		if(request()->isPost()){
			$data = input('post.');
			$data['addtime'] = time();
			if($_FILES['thumb']['tmp_name']){
				$oldSrc = IMG_UPLOAD.$dataRes['thumb'];
				if(file_exists($oldSrc)){
					@unlink($oldSrc);
				}
				$data['thumb'] = $this->upload();
			}
			$editRes = db('article')->update($data);
			if($editRes !== false){
				$this->success('文章修改成功！',url('admin/Article/index'));
			}else{
				$this->error('文章修改失败！');
			}
		}
		$cateRes = db('cate')->order("sort desc")->select();
		$cateRes = $cate->catetree($cateRes);
		$this->assign([
			'cateRes'	=>$cateRes,
			'dataRes'	=>$dataRes,
		]);
		return view();
	}

	public function del($id){
		$dataRes = db('article')->find($id);
		$thumbSrc = IMG_UPLOAD.$dataRes['thumb'];
		if(file_exists($thumbSrc)){
			@unlink($thumbSrc);
		}
		$res = db('article')->delete($id);
		if($res){
			$this->success('文章删除成功！',url('admin/Article/index'));
		}else{
			$this->error('文章删除失败！');
		}
	}

	//上传图片
	public function upload(){
	    // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('thumb');
	    
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'static'. DS .'upload');
	        if($info){
	            return $info->getSaveName();
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	            die;
	        }
	    }
	}

	public function imglist(){
		$_files=my_scandir();
		$files=array();
		foreach ($_files as $k => $v) {
		  if(is_array($v)){
		    foreach ($v as $k1 => $v1) {
		    	$str = ROOT_PATH.'public';
		    	$v1=str_replace($str,'',$v1);
		        $files[]=$v1;
		    }
		  }else{
		  	$str = ROOT_PATH.'public';
		  	$v=str_replace($str,'', $v);
		    $files[]=$v;
		  }
		}
		// dump($files);die;
		$this->assign([
		    'imgRes'=>$files,
		    ]);
		return view();
	}

	public function delimg(){
		if(request()->isAjax()){
			$imgsrc=input('imgsrc');
			$imgsrc=ROOT_PATH.'public'.$imgsrc;
			if(file_exists($imgsrc)){
			    if(@unlink($imgsrc)){
			        echo 1;
			    }else{
			        echo 2;
			    }
			}else{
			    echo 3;
			}
		}
	}

}